#include "RegGrammaticaToNonFinAut.h"

NonFinAut<string> RegGrammaticaToNonFinAut::convertToNonFinAut(RegGrammatica<string> &regGrammatica)
{
	//conversie naar NFA volgens stelling 3.9
	string finishState = "F";	//extra toestand die als eindtoestand zal fungeren
	
	list<string> finishStateList;
	finishStateList.push_back(finishState);

	list<string> nonTerminals = regGrammatica.getNonTerminals();
	list<string> nonTerminalsList;
	
	list<ProductionRule<string> > productionRules = regGrammatica.getProductionRules();
	list<string> startSymbol;
	string start = regGrammatica.getStartSymbol();
	string startSymb = string("q") + start[1];
	startSymbol.push_back(startSymb);
	NonFinAut<string> nonFinAut(startSymbol, finishStateList);
	list<ProductionRule<string> >::iterator i;
	for(i = productionRules.begin(); i != productionRules.end(); i++)
	{
		ProductionRule<string> productionRule = *i;
		string left = i->getLeftNonTerminal();
		
		string leftNonTerminalRewrited = string("q") + left[1];
		if(productionRule.getIfEnd())
		{	
			nonFinAut.addTransition(leftNonTerminalRewrited, productionRule.getRightTerminal(), finishState);
		}
		else
		{
			string right = i->getRightNonTerminal();
			string rightNonTerminalRewrited = string("q") + right[1];
			nonFinAut.addTransition(leftNonTerminalRewrited, productionRule.getRightTerminal(), rightNonTerminalRewrited);
		}
	}

	nonFinAut.rewrite();
	return nonFinAut;
}
